Page({
  data: {
    // 站点列表，托山作为第一个默认选项
    siteList: [
      { text: '托山', value: '托山' },
      { text: '乌龟山南', value: '乌龟山南' },
      { text: '胥湖心', value: '胥湖心' },
      { text: '五里湖心', value: '五里湖心' },
      { text: '锡东水厂', value: '锡东水厂' },
      { text: '兰山嘴', value: '兰山嘴' }
    ],
    currentSite: '托山', // 明确设置默认站点为托山
    
    // 存储所有站点的数据
    allSitesData: {},
    
    // 当前站点显示的数据
    currentDensity: 0,
    density1h: 0,
    density2h: 0,
    updateTime: '',
    prediction1h: 0,
    prediction2h: 0,
    prediction3h: 0,
    prediction4h: 0,
    historyList: [],
    searchDate: ''
  },

  onLoad() {
    // 先从本地存储获取数据
    const storedData = wx.getStorageSync('algaeData');
    
    if (storedData && storedData.allSitesData) {
      // 如果有存储的数据，直接使用
      this.setData(storedData);
      // 强制加载托山站点数据（确保默认显示）
      this.loadSiteData('托山');
    } else {
      // 初始化所有站点数据，并默认显示托山
      this.initAllSitesData();
    }
  },

  // 初始化所有站点的数据
  initAllSitesData() {
    const allSitesData = {};
    const updateTime = new Date().toLocaleString('zh-CN', { 
      year: 'numeric', month: '2-digit', day: '2-digit',
      hour: '2-digit', minute: '2-digit'
    }).replace(/\//g, '-');
    
    // 为每个站点生成独立数据
    this.data.siteList.forEach(site => {
      allSitesData[site.value] = this.generateSiteData(updateTime);
    });
    
    this.setData({
      allSitesData: allSitesData,
      updateTime: updateTime
    });
    
    // 明确加载托山站点数据作为初始显示
    this.loadSiteData('托山');
    
    // 保存数据到本地存储
    wx.setStorageSync('algaeData', this.data);
  },

  // 为单个站点生成数据（5000-8000之间）
  generateSiteData(updateTime) {
    // 生成实时数据
    const current = Math.floor(Math.random() * 3000) + 5000;
    const density1h = Math.floor(Math.random() * 3000) + 5000;
    const density2h = Math.floor(Math.random() * 3000) + 5000;
    
    // 生成预测数据
    const prediction1h = Math.floor(Math.random() * 3000) + 5000;
    const prediction2h = Math.floor(Math.random() * 3000) + 5000;
    const prediction3h = Math.floor(Math.random() * 3000) + 5000;
    const prediction4h = Math.floor(Math.random() * 3000) + 5000;
    
    // 生成历史数据
    const historyList = [];
    for (let i = 1; i <= 7; i++) {
      const date = new Date();
      date.setDate(date.getDate() - i);
      historyList.push({
        date: date.toLocaleDateString('zh-CN'),
        density: Math.floor(Math.random() * 3000) + 5000,
        time: `${Math.floor(Math.random() * 24)}:${Math.floor(Math.random() * 60).toString().padStart(2, '0')}`
      });
    }
    
    return {
      currentDensity: current,
      density1h: density1h,
      density2h: density2h,
      prediction1h: prediction1h,
      prediction2h: prediction2h,
      prediction3h: prediction3h,
      prediction4h: prediction4h,
      historyList: historyList,
      updateTime: updateTime
    };
  },

  // 加载指定站点的数据
  loadSiteData(siteName) {
    const siteData = this.data.allSitesData[siteName];
    if (siteData) {
      this.setData({
        currentSite: siteName,
        currentDensity: siteData.currentDensity,
        density1h: siteData.density1h,
        density2h: siteData.density2h,
        prediction1h: siteData.prediction1h,
        prediction2h: siteData.prediction2h,
        prediction3h: siteData.prediction3h,
        prediction4h: siteData.prediction4h,
        historyList: siteData.historyList,
        updateTime: siteData.updateTime
      });
    }
  },

  // 判断趋势变化
  getTrend(previous, current) {
    const diff = current - previous;
    if (diff > 0) return `↑ 上升 ${diff} 个/L`;
    if (diff < 0) return `↓ 下降 ${Math.abs(diff)} 个/L`;
    return '→ 保持不变';
  },

  // 切换站点
  onSiteChange(e) {
    const newSite = e.detail;
    this.loadSiteData(newSite);
    // 更新本地存储中的当前站点
    wx.setStorageSync('algaeData', this.data);
  },



  // 返回上一页
  onClickLeft() {
    wx.navigateBack({ delta: 1 });
  }
});
    